(() => {
  // 创建 Vue 实例并挂载到 id 为 main 的元素上
  new Vue({
    el: '#main',
    mixins: [mixin],
    data: {
      testName:"刘动",
      patientListURL: '/patient/list',
      addPatientURL: '',
      doctorTestData: {
        doctorInfoId: 77,
        doctorName: '赵xx',
        doctorType: "doctorInCharge" // doctorInCharge-主管医生; dutyDoctor-值班医生
      },
      patientsTestData: [
        //  {
        //   // isComfirmWarning: "1",
        //   // name: "赵丽"
        //   // patientId: "3516"
        //   // patientInfoId: 50
        //   // warningId: "1"
        // },
      ]
    },
    created () {
      // 初始进入页面需要做的动作
      // 获取当前登陆人身份信息
      // 获取当前登陆人名下的患者列表
      let data = this.base64Encode(this.testName)
      console.log(data,"加密")
      console.log(this.base64Decode(data),"解码")

      this.setData()
      this.initData()
    },
    methods: {
      initData () {
        // 获取登陆后存在session的数据  
        // let data = sessionStorage.getItem("doctorTestData")
        let url_ = window.location.href
        let data = this.getUrlParams(url_)
        if (data) {
          Object.assign(this.doctorTestData,data)
          console.log(this.doctorTestData,"222")
          sessionStorage.setItem("doctorTestData",JSON.stringify(data))
          // 查询当前医生名下患者列表
          const { doctorInfoId, doctorName, doctorType } = this.doctorTestData
          this.getPatientsData(doctorInfoId, doctorName, doctorType)
        } else {
          console.log("未获取到登陆人医生信息")
        }
      },
      
      //预警的颜色
      getOperation (operation) {
        switch (operation) {
          case 'no':
            return 'none';
          case 'red':
            return '#cd0c0c';
          case 'yellow':
            return "#d0ca0f";
          default:
            return 'none';
        }
      },
      // 值班 主治切换功能
      async changeDoctor () {
        // 切换时查询当前医生名下的患者列表
        // 变更获得医生信息 更新后端医生信息 和本地医生信息存储  再次更新患者列表
        await this.switchDoctorRole(this.doctorTestData.doctorType == "doctorInCharge" ? "dutyDoctor" : "doctorInCharge")
        const { doctorInfoId, doctorName, doctorType } = this.doctorTestData
        this.getPatientsData(doctorInfoId, doctorName, doctorType)
      },
      async switchDoctorRole (doctorType) {
        try {
          const url = "/doctor/switch?doctorType=" + doctorType;
          const response = await fetch(apiUrl + url, {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            // body: JSON.stringify({
            //   doctorType: doctorType,
            // })
          })
          const data = await response.json()
          if (data.code === 200) {
            if (this.doctorTestData.doctorType == "doctorInCharge") {
              this.doctorTestData.doctorType = "dutyDoctor"
            } else if (this.doctorTestData.doctorType == "dutyDoctor") {
              this.doctorTestData.doctorType = "doctorInCharge"
            }

            sessionStorage.setItem("doctorTestData", JSON.stringify(this.doctorTestData))
          }
          // .then(response => response.json())
          // .then(data => {
          //   if (data.code === 200) {
          //     if (this.doctorTestData.doctorType == "doctorInCharge") {
          //       this.doctorTestData.doctorType = "dutyDoctor"
          //     } else if (this.doctorTestData.doctorType == "dutyDoctor") {
          //       this.doctorTestData.doctorType = "doctorInCharge"
          //     }
          //     const { doctorInfoId, doctorName, doctorType } = this.doctorTestData
          //     sessionStorage.setItem("doctorTestData", JSON.stringify(this.doctorTestData))
          //     this.getPatientsData(doctorInfoId, doctorName, doctorType)
          //   } else {
          //     console.error('Failed to fetch patients:', data.msg);
          //   }
          // })
          // .catch(error => console.error('Error:', error));
        } catch (error) {
          console.error('Error:', error)
        }

      },
      // 点击患者
      patientHnader (e) {
        console.log(e, "当前人员信息")
        // 存储当前点击的患者信息 id 存到sessionStorage中  跳转页面
        sessionStorage.setItem("patientInfoId", e.patientInfoId)
        // 携带患者id跳转到患者详情页做查询操作
        window.location.href = "basicInfo.html"
      },
      //点击新增按钮
      addpatientHnader () {
        // 携带信息做跳转处理
        window.location.href = "addPatient.html"
        // const url = addPatientURL;
        // fetch(url, {
        // 	method: "POST",
        // 	headers: {
        // 		'Content-Type': 'application/json'
        // 	},
        // 	body: JSON.stringify({
        // 		docterId: docterId,
        // 		doctorName: doctorName,
        // 		doctorType: doctorType
        // 	})
        // })

      },
      // 获取患者列表接口
      /**
       * 
       获取患者列分页内容查询  /patient/list
       {
          "pageNum":"int //当前页码【必须】",
          "pageSize":"int //每页条数【必须】",
          "orderBy":"string //排序方式,可以是一个字符串如:id desc",
          "doctorInfoId":"string //医生主键id【必须】",
          "doctorName":"string //医生名称【必须】",
          "doctorType":"string //医生类别\doctorInCharge-主管医生; dutyDoctor-值班医生\t当为值班医生时,不过滤所属医生【必须】"
       }
       */
      getPatientsData (doctorInfoId, doctorName, doctorType) {
        const url = this.patientListURL;

        fetch(apiUrl + url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            doctorInfoId: doctorInfoId,
            doctorName: doctorName,
            doctorType: doctorType,
            pageNum: 1,
            pageSize: 999,//先查询所有数据暂不分页处理
            orderBy: "pi.patient_info_id"
          })
        })
          .then(response => response.json())
          .then(data => {
            if (data.code === 200) {
              // 请求成功更新患者列表对象
              this.patientsTestData = data.data.list ?? []
              // loadPatientList(data.data);
            } else {
              console.error('Failed to fetch patients:', data.msg);
            }
          })
          .catch(error => console.error('Error:', error));

      }

    }
  });
})();
